//
//  ProductListController.m
//  ZSBTC
//
//  Created by DJ on 17/1/10.
//  Copyright © 2017年 DJ. All rights reserved.
//

#import "ProductListController.h"

#import "ProductController.h"
#import "ProductListCell.h"
#import "ShoppingCartBtn.h"
#import "HistoryController.h"
#import "CartViewController.h"



@interface ProductListController ()

@property (nonatomic,strong)ShoppingCartBtn *CartSBtn;
@property (nonatomic,assign)NSInteger cnt;      // 记录个数


@property (nonatomic,strong) UIButton *footBtn;

@end

@implementation ProductListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavBar:NavBarType_BackHideen_No title:self.title];
    
    [self LoadTableView];
    
    [self.CartSBtn bringSubviewToFront:self.tableView];
    [self.view addSubview:self.CartSBtn];
    
    [self.footBtn bringSubviewToFront:self.tableView];
    [self.view addSubview:self.footBtn];
}

- (void)LoadTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBar_Hight, Screen_Width, Screen_Height-NavBar_Hight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ProductListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ProductListCell class])];
}

- (UIButton *)footBtn{
    if (!_footBtn) {
        _footBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _footBtn.frame = CGRectMake(Screen_Width-70,90, 50, 50);
        [_footBtn setImage:[UIImage imageNamed:@"foot"] forState:UIControlStateNormal];
        [_footBtn addTarget:self action:@selector(footBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _footBtn;
}

- (void)footBtnClick:(UIButton*)button{
    ZFLog(@"足迹点击了");
    
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Action" bundle:nil];
    HistoryController *Vc = [story instantiateViewControllerWithIdentifier:@"HistoryController"];
    Vc.title = @"购物足迹";
    [self.navigationController pushViewController:Vc animated:YES];
}


- (UIButton*)CartSBtn{
    if (!_CartSBtn) {
        _CartSBtn = [[ShoppingCartBtn alloc] initWithFrame:CGRectMake(Screen_Width-80, Screen_Height-120, 50, 50)];
        [_CartSBtn setBtnAction:@selector(CartSBtnClick:) delegate:self];
    }
    return _CartSBtn;
}

- (void)CartSBtnClick:(ShoppingCartBtn*)button{
    ZFLog(@"你好 世界");
    
    UIStoryboard* story = [UIStoryboard storyboardWithName:@"Cart" bundle:nil];
    CartViewController* Vc = [story instantiateViewControllerWithIdentifier:@"CartViewController"];
    Vc.title = @"购物车";
    Vc.navBarType = NavBarType_BackHideen_No;
    Vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:Vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark -
#pragma mark--UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ProductListCell *cell = [ProductListCell msGetInstance];
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *strsss = @"加购\n物车";
    return strsss;
    
}




/* 编辑模式下调用 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ProductListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    __block typeof(self)BlockSelf = self;
    tableView.editing = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [BlockSelf addProductsAnimation:cell.produtImg dropToPoint:CGPointMake(Screen_Width-50, BlockSelf.view.layer.bounds.size.height - 120) isNeedNotification:YES];
        BlockSelf.addShopCarFinished = ^{
            NSLog(@"完成了动画（如果不使用通知的方式，可以使用这种方式）");
            self.cnt++;
            [self.CartSBtn setCnt:_cnt];
            
        };
    });
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIStoryboard* story = [UIStoryboard storyboardWithName:@"Category" bundle:nil];
    ProductController* Vc = [story instantiateViewControllerWithIdentifier:@"ProductController"];
    [self.navigationController pushViewController:Vc animated:YES];
}


@end
